/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.view.student;

import foxmob.mlts.model.FillInTheBlanksQuestion;
import foxmob.mlts.viewcontroller.student.QuestionPaperForm;

/**
 *
 * @author zpf16z
 */
public class FillInTheBlankPanel extends javax.swing.JPanel {
    
    private FillInTheBlanksQuestion fib;
    public int questionCount;
    boolean state = false;

    /**
     * Creates new form FillInBlankJPanel
     */
    public FillInTheBlankPanel() {
        initComponents();
    }
    
    public void loadQuestion(FillInTheBlanksQuestion q, int count){
        this.fib = q;
        this.questionCount = count;
        this.jLabelQuestionID.setText("  Q" + Integer.toString(count) + ":");
        this.jLabelQuestion.setText(q.getQuestionText());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelQuestion = new javax.swing.JLabel();
        jLabelQuestionID = new javax.swing.JLabel();
        jTextFieldAnswer = new javax.swing.JTextField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabelQuestion.setText("This is a question!!!!!!!!!!!!!!!!!!!!!!");
        add(jLabelQuestion, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 10, -1, 20));

        jLabelQuestionID.setText("Q1.");
        add(jLabelQuestionID, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 20));

        jTextFieldAnswer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldAnswerActionPerformed(evt);
            }
        });
        jTextFieldAnswer.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldAnswerFocusLost(evt);
            }
        });
        jTextFieldAnswer.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jTextFieldAnswerInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });
        jTextFieldAnswer.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTextFieldAnswerPropertyChange(evt);
            }
        });
        jTextFieldAnswer.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldAnswerKeyTyped(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldAnswerKeyReleased(evt);
            }
        });
        add(jTextFieldAnswer, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 170, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldAnswerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldAnswerFocusLost
        // TODO add your handling code here:
//        setAnswer();
        
    }//GEN-LAST:event_jTextFieldAnswerFocusLost

    private void jTextFieldAnswerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAnswerActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTextFieldAnswerActionPerformed

    private void jTextFieldAnswerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTextFieldAnswerPropertyChange
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTextFieldAnswerPropertyChange

    private void jTextFieldAnswerInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTextFieldAnswerInputMethodTextChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldAnswerInputMethodTextChanged

    private void jTextFieldAnswerKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldAnswerKeyTyped
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTextFieldAnswerKeyTyped

    private void jTextFieldAnswerKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldAnswerKeyReleased
        // TODO add your handling code here:
        setAnswer();
    }//GEN-LAST:event_jTextFieldAnswerKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelQuestion;
    private javax.swing.JLabel jLabelQuestionID;
    private javax.swing.JTextField jTextFieldAnswer;
    // End of variables declaration//GEN-END:variables

    public void setFIB(FillInTheBlanksQuestion q) {
        this.fib = q;
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void setAnswer() {
        //fib = (FillInTheBlanksQuestion) QuestionPaperForm.testRecord.getAllQuestions().get(this.questionCount - 1);
        fib.setStudentAnswer(this.jTextFieldAnswer.getText());
        setQuestionFinished();
    }

    private void setQuestionFinished() {
        if (this.state) {
            if (this.jTextFieldAnswer.getText().length() == 0) {
                this.state = false;
                QuestionPaperForm.finishedQuestions--;
            }
        } else {
            if (this.jTextFieldAnswer.getText().length() > 0) {   
                this.state = true;
                QuestionPaperForm.finishedQuestions++;
            }
        }
    }
}
